#include <stdio.h>

int main()
{
    long long A, B, ans;
    while (scanf("%lld%lld", &A, &B) == 2)
    {
        if (A == 0 && B == 0)
        {
            break;
        }
        ans = 1;
        while (B)
        {
            if (B & 1)
            {
                ans = ans * A % 1000;
            }
            B >>= 1;
            A = (A * A) % 1000;
        }
        printf("%lld\n", ans);
    }
    return 0;
}